from Crypto.Cipher import AES
from Crypto.Util.Padding import unpad
from gmssl.sm4 import CryptSM4, SM4_DECRYPT
import base64


def decrypt(e: str, t: str = None, client_encrypt: str = "0", u="", l="", s=""):
    """
    对应 JS 逻辑的 Python 实现
    e: 密文（base64）
    t: AES 密钥 (如果为 None，使用 u+l+s)
    client_encrypt: 用于判断算法类型（最后一位）
    """
    c = client_encrypt[-1]  # JS 中取最后一位
    r = ""
    if c == "0":  # AES 解密
        if t is None:
            t = f"{u}{l}{s}"
        key = t.encode('utf-8')

        cipher = AES.new(key, AES.MODE_ECB)
        decrypted_data = cipher.decrypt(base64.b64decode(e))
        r = unpad(decrypted_data, AES.block_size).decode('utf-8')
    elif c == "1":  # SM4 解密
        key = b"MbzgvXzBWynQrtpy"
        iv = b"kDrvPQfPIuArAzkF"
        crypt_sm4 = CryptSM4()
        crypt_sm4.set_key(key, SM4_DECRYPT)
        decrypted_data = crypt_sm4.crypt_cbc(iv, base64.b64decode(e))
        r = decrypted_data.decode('utf-8')
    return r


import requests

headers = {
    "Accept": "application/json, text/javascript, */*; q=0.01",
    "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
    "Auth-Plus;": "",
    "Cache-Control": "no-cache",
    "Client-Encrypt": "v1.1",
    "Connection": "keep-alive",
    "Origin": "https://www.hanghangcha.com",
    "Pragma": "no-cache",
    "Sec-Fetch-Dest": "empty",
    "Sec-Fetch-Mode": "cors",
    "Sec-Fetch-Site": "same-site",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36 Edg/142.0.0.0",
    "X-Requested-With": "XMLHttpRequest",
    "clientInfo": "web",
    "clientVersion": "1.0.6",
    "currentHref": "https://www.hanghangcha.com/hhcreport",
    "sec-ch-ua": "\"Chromium\";v=\"142\", \"Microsoft Edge\";v=\"142\", \"Not_A Brand\";v=\"99\"",
    "sec-ch-ua-mobile": "?0",
    "sec-ch-ua-platform": "\"Windows\""
}
cookies = {
    "Hm_lvt_1521e0fb49013136e79181f2888214a7": "1762331055",
    "Hm_lpvt_1521e0fb49013136e79181f2888214a7": "1762331055",
    "HMACCOUNT": "06D1F1816DD4C515",
    "JSESSIONID": "784C7ADA7F3B277143BE9BC1A45ACE20",
    "_ACCOUNT_": "OWRlZjM4MzQ2NGRkNDU2OWJkOTRhN2M3NWUyMTFhMjclNDAlNDBtb2JpbGU6MTc2MzU0MTAxMjE2MjpjMDNiMjk5MGM0ZjdhMTNjYmNlZDQ2ZGVjNjU3YzBlYQ"
}
url = "https://api.hanghangcha.com/hhc/member/industry/getReportList"
params = {
    "filter": "{\"reportType\":null,\"limit\":10,\"skip\":0}"
}
response = requests.get(url, headers=headers, cookies=cookies, params=params).json()
print(decrypt(response['data'], client_encrypt="clientEncrypt1"))
